package com.victualler.product.goods.service.impl;

import com.victualler.core.config.dict.entity.DictDao;
import com.victualler.core.config.dict.service.DictService;
import com.victualler.product.goods.dto.GoodsDto;
import com.victualler.product.goods.entity.GoodsImgDetailDao;
import com.victualler.product.goods.entity.GoodsItemDetailDao;
import com.victualler.product.goods.entity.GoodsModelDetailDao;
import com.victualler.product.goods.entity.GoodsTypeDetailDao;
import com.victualler.product.goods.repository.GoodsMainRepository;
import com.victualler.product.goods.service.GoodsImgDetailService;
import com.victualler.product.goods.service.GoodsItemDetailService;
import com.victualler.product.goods.service.GoodsModelDetailService;
import com.victualler.product.goods.service.GoodsService;
import com.victualler.product.goods.service.GoodsTypeDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Transient;

@Service
public class GoodsServiceImpl implements GoodsService{
	
	@Autowired
	private GoodsMainRepository  goodsMainRepository;
	@Autowired
	private GoodsImgDetailService goodsImgDetailService;
	@Autowired
	private GoodsItemDetailService goodsItemDetailService;
	@Autowired
	private GoodsModelDetailService goodsModelDetailService; 
	@Autowired
	private GoodsTypeDetailService goodsTypeDetailService;
	@Autowired
	private DictService dictService;
	
	@Override
	public GoodsDto getByGoodsId(Long goodsId) {
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setGoodsMainDao(goodsMainRepository.getOne(goodsId));
		goodsDto.setGoodsImgDetailDaoList(goodsImgDetailService.getByParentId(goodsId));
		goodsDto.setGoodsItemDetailDaoList(goodsItemDetailService.getByParentId(goodsId));
		goodsDto.setGoodsModelDetailDaoList(goodsModelDetailService.getByParentId(goodsId));
		goodsDto.setGoodsTypeDetailDaoList(goodsTypeDetailService.getByParentId(goodsId));
		//设置字典值
		for (GoodsTypeDetailDao goodsTypeDetailDao : goodsDto.getGoodsTypeDetailDaoList()) {
			DictDao res = dictService.getDictByCodeType("CustomerTag",goodsTypeDetailDao.getType());
			goodsTypeDetailDao.setTypeName(res.getCodeValue());
		}
		return goodsDto;
	}

	@Transient
	@Override
	public void save(GoodsDto goodsDto) {
		//设置主图
		for(GoodsImgDetailDao goodsImgDetailDao : goodsDto.getGoodsImgDetailDaoList()){
            if(goodsImgDetailDao.getPlace() == 0){ //主图
            	goodsDto.getGoodsMainDao().setImgId(goodsImgDetailDao.getImgId());
                break;
            }
        }
		goodsMainRepository.save(goodsDto.getGoodsMainDao());
		//子表设置父主键
		for(GoodsImgDetailDao goodsImgDetailDao : goodsDto.getGoodsImgDetailDaoList()){
			goodsImgDetailDao.setParentId(goodsDto.getGoodsMainDao().getId());
        }
		for(GoodsItemDetailDao goodsItemDetailDao : goodsDto.getGoodsItemDetailDaoList()){
			goodsItemDetailDao.setParentId(goodsDto.getGoodsMainDao().getId());
        }
		for(GoodsModelDetailDao goodsModelDetailDao : goodsDto.getGoodsModelDetailDaoList()){
			goodsModelDetailDao.setParentId(goodsDto.getGoodsMainDao().getId());
        } 
		for(GoodsTypeDetailDao goodsTypeDetailDao : goodsDto.getGoodsTypeDetailDaoList()){
			goodsTypeDetailDao.setParentId(goodsDto.getGoodsMainDao().getId());
        } 
		goodsImgDetailService.saveAll(goodsDto.getGoodsImgDetailDaoList());
		goodsItemDetailService.saveAll(goodsDto.getGoodsItemDetailDaoList());
		goodsModelDetailService.saveAll(goodsDto.getGoodsModelDetailDaoList());
		goodsTypeDetailService.saveAll(goodsDto.getGoodsTypeDetailDaoList());
	}

	@Transient
	@Override
	public void updateGoodsDto(GoodsDto goodsDto) {
		//重置主图
		for(GoodsImgDetailDao goodsImgDetailDao : goodsDto.getGoodsImgDetailDaoList()){
			if(goodsImgDetailDao.getPlace() == 0){ //主图
				goodsDto.getGoodsMainDao().setImgId(goodsImgDetailDao.getImgId());
				break;
			}
		}
		Long parentId = goodsDto.getGoodsMainDao().getId();
		goodsMainRepository.save(goodsDto.getGoodsMainDao());
		goodsImgDetailService.updateList(goodsDto.getGoodsImgDetailDaoList(),parentId);
		goodsItemDetailService.updateList(goodsDto.getGoodsItemDetailDaoList(),parentId);
		goodsModelDetailService.updateList(goodsDto.getGoodsModelDetailDaoList(),parentId);
		goodsTypeDetailService.updateList(goodsDto.getGoodsTypeDetailDaoList(),parentId);
	}


	/**
	 * @author Guoshichang
	 * @since 21:05 on 2018/12/7.
	 * @Function  统一刷条形码
	 */
	public void flushModelBarId() {
		this.goodsModelDetailService.flushModelBarId();
	}
}
